/**
 * Created by gengrong on 16/5/13.
 */
import { createReducer } from './util';
import * as RunnableGroupAction from '../actions/runnable-group';
import * as RunnableUnitAction from '../actions/runnable-unit';
import _ from 'lodash';

function updateUnit(state, unitId, data) {
  const oldUnit = state[unitId] || {};
  const newUnit = Object.assign({}, oldUnit, data);
  return Object.assign({}, state, { [unitId]: newUnit });
}
export const runnableUnitsById = createReducer(on => {
  on(RunnableGroupAction.listUnitsByRuntimeId)
    .completed((state, action) => {
      const unitsMap = {};
      action.payload.forEach(unit => {
        unitsMap[unit.id] = unit;
      });
      return Object.assign({}, state, unitsMap);
    });
  // TODO 下面几个 actions 的 runtimeId meta 都被移除了，从相关代码中去掉
  on(RunnableUnitAction.updateUnitMachines)
    .completed((state, action) => {
      const { unitId } = action.meta;
      return updateUnit(state, unitId, { machineList: action.payload });
    });
  on(RunnableUnitAction.updateUnitBuildConf)
    .completed((state, action) => {
      const { unitId } = action.meta;
      return updateUnit(state, unitId, action.payload);
    });
  on(RunnableUnitAction.updateUnitType)
    .completed((state, action) => {
      const { unitId } = action.meta;
      return updateUnit(state, unitId, { type: action.payload });
    });
  on(RunnableUnitAction.updateUnitSettings)
    .completed((state, action) => {
      const { unitId } = action.meta;
      return updateUnit(state, unitId, action.payload);
    });
}, {});
export const runnableUnitIdsByRuntimeId = createReducer(on => {
  on(RunnableGroupAction.listUnitsByRuntimeId)
    .completed((state, action) => {
      return Object.assign({}, state, { [action.meta.runtimeId]: action.payload.map(unit => unit.id) });
    });
}, {});
export const machineStatusListByUnitId = createReducer(on => {
  on(RunnableUnitAction.listMachineStatusByUnitId)
    .completed((state, action) => {
      const { unitId } = action.meta;
      return Object.assign({}, state, { [unitId]: action.payload });
    });
}, {});

export const successBuildListByUnit = createReducer(on => {
  on(RunnableUnitAction.listSuccessBuildsByUnit)
    .completed((state, action) => {
      const unitId = action.meta.unitId;
      return Object.assign({}, state, { [unitId]: action.payload });
    });
}, {});

export const deployLogListByUnit = createReducer(on => {
  on(RunnableUnitAction.listDeployLogByUnit)
    .completed((state, action) => {
      const unitId = action.meta.unitId;
      return Object.assign({}, state, { [unitId]: action.payload });
    });
  on(RunnableUnitAction.updateDeployLogStatus)
    .completed((state, action) => {
      if (_.isEmpty(state)) {
        return state;
      }
      const logList = state[action.payload.unitId];
      if (_.isEmpty(logList)) {
        return state;
      }
      const deploymentLogList = logList.map(log => {
        if (log.id !== action.payload.id) {
          return log;
        }
        return Object.assign({}, log, action.payload);
      });
      state[action.payload.unitId] = deploymentLogList;
      return Object.assign({}, state);
    });
}, []);
